package com.example.yichuangyun.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemCallRecordBinding;
import com.example.yichuangyun.databinding.ItemFollowRecordBinding;
import com.example.yichuangyun.databinding.LayoutEmptyList2Binding;
import com.example.yichuangyun.databinding.LayoutEmptyListBinding;
import com.example.yichuangyun.model.business.FollowDataModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 跟进
 */
public class FollowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * viewType--分别为item以及空view
     */
    private final int VIEW_TYPE_ITEM = 1;
    private static final int VIEW_TYPE_EMPTY = 0;
    private final Context context;
    private List<FollowDataModel.FollowRecordModel> mData;
    private boolean isOpen;
    /**
     * 0列表、1详情列表
     */
    private final int flag;

    public FollowAdapter(Context context, List<FollowDataModel.FollowRecordModel> mData, int flag) {
        this.context = context;
        this.mData = mData;
        this.flag = flag;
    }

    public void onRefresh(List<FollowDataModel.FollowRecordModel> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    public void addData(List<FollowDataModel.FollowRecordModel> mList) {
        if (mList != null && mList.size() != 0) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //在这里根据不同的viewType进行引入不同的布局
        if (viewType == VIEW_TYPE_EMPTY) {
            LayoutEmptyList2Binding emptyListBinding = LayoutEmptyList2Binding.inflate(LayoutInflater.from(context), parent, false);
            return new EmptyViewHolder(emptyListBinding);
        } else {
            ItemCallRecordBinding itemFollowRecordBinding = ItemCallRecordBinding.inflate(LayoutInflater.from(context), parent, false);
            return new MyViewHolder(itemFollowRecordBinding);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            FollowDataModel.FollowRecordModel model = mData.get(position);
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            if (flag == 1) {
                LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) myViewHolder.binding.tvTopTime.getLayoutParams();
                linearParams.topMargin = 0;
                linearParams.height = ScreenUtil.dip2px(25);
                myViewHolder.binding.tvTopTime.setLayoutParams(linearParams);
                if (position == 0) {
                    myViewHolder.binding.itemView.setPadding(0, ScreenUtil.dip2px(15), 0, 0);
                }
            }
            myViewHolder.binding.tvTopTime.setText(CommonUtil.checkText(model.getTelDate()));
            if (position == 0) {
                myViewHolder.binding.tvTopTime.setVisibility(View.VISIBLE);
            }
            if (position > 0) {
                String date1 = model.getTelDate();
                String date2 = mData.get(position - 1).getTelDate();
                if (!TextUtils.isEmpty(date1) && !TextUtils.isEmpty(date2)) {
                    myViewHolder.binding.tvTopTime.setVisibility(date1.equals(date2) ? View.GONE : View.VISIBLE);
                }
            }

            myViewHolder.binding.recyclerView.setAdapter(new MyFollowAdapter(mData.get(position).getChildRecords()));
        } else if (holder instanceof EmptyViewHolder) {
            EmptyViewHolder emptyHolder = (EmptyViewHolder) holder;
            emptyHolder.binding.text.setText(context.getString(R.string.no_follow));
        }
    }

    class MyFollowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final List<FollowDataModel.FollowRecordModel.FollowModel> mData;

        public MyFollowAdapter(List<FollowDataModel.FollowRecordModel.FollowModel> mData) {
            this.mData = mData;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemFollowRecordBinding itemFollowRecordBinding = ItemFollowRecordBinding.inflate(LayoutInflater.from(context), parent, false);
            return new FollowViewHolder(itemFollowRecordBinding);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof FollowViewHolder) {
                FollowViewHolder myViewHolder = (FollowViewHolder) holder;
                FollowDataModel.FollowRecordModel.FollowModel model = mData.get(position);
                myViewHolder.binding.tvName.setText(String.format(context.getString(flag == 0 ? R.string.contacts_person_mao : R.string.follow_person_mao), flag == 0 ? model.getFollowName() : model.getPresentGdUser()));
                if (!TextUtils.isEmpty(model.getFollowTime()) && model.getFollowTime().length() >= 15) {
                    myViewHolder.binding.tvTime.setText(model.getFollowTime().substring(11, 16));
                }
                myViewHolder.binding.tvFollowNextTime.setText(String.format(context.getString(R.string.next_follow_time3), CommonUtil.setYHMTime(model.getNextFollowTime())));
                myViewHolder.binding.tvFollowContent.setText(!TextUtils.isEmpty(model.getFollowRemark()) ? model.getFollowRemark() : context.getString(R.string.no));
                myViewHolder.binding.tvOpen.post(() -> myViewHolder.binding.tvOpen.setVisibility(myViewHolder.binding.tvFollowContent.getLineCount() > 2 ? View.VISIBLE : View.GONE));
                myViewHolder.binding.tvType.setText(CommonUtil.checkText(model.getFollowTypeName()));
                if (model.getFollowType() == 5) {
                    myViewHolder.binding.llViewInfo.setVisibility(View.VISIBLE);
                    myViewHolder.binding.tvFollowAddress.setText(String.format(context.getString(R.string.face_address), model.getViewAddress()));
                    if (!TextUtils.isEmpty(model.getViewFile())) {
                        Glide.with(context).load(model.getViewFile()).into(myViewHolder.binding.ivImg);
                    }
                } else {
                    myViewHolder.binding.llViewInfo.setVisibility(View.GONE);
                }

                myViewHolder.binding.tvOpen.setOnClickListener(v -> {
                    isOpen = !isOpen;
                    if (isOpen) {
                        myViewHolder.binding.tvOpen.setText(context.getString(R.string.no_expand));
                        myViewHolder.binding.tvFollowContent.setMaxLines(Integer.MAX_VALUE);
                    } else {
                        myViewHolder.binding.tvOpen.setText(context.getString(R.string.expand));
                        myViewHolder.binding.tvFollowContent.setMaxLines(2);
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    @Override
    public int getItemCount() {
        //同时这里也需要添加判断，如果mData.size()为0的话，只引入一个布局，就是emptyView
        // 那么，这个recyclerView的itemCount为1
        if (mData.size() == 0) {
            return 1;
        }
        //如果不为0，按正常的流程跑
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        //在这里进行判断，如果我们的集合的长度为0时，我们就使用emptyView的布局
        if (mData.size() == 0) {
            return VIEW_TYPE_EMPTY;
        }
        //如果有数据，则使用ITEM的布局
        return VIEW_TYPE_ITEM;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemCallRecordBinding binding;

        public MyViewHolder(ItemCallRecordBinding itemFollowRecordBinding) {
            super(itemFollowRecordBinding.getRoot());
            binding = itemFollowRecordBinding;
        }
    }

    static class EmptyViewHolder extends RecyclerView.ViewHolder {

        LayoutEmptyList2Binding binding;

        public EmptyViewHolder(LayoutEmptyList2Binding emptyListBinding) {
            super(emptyListBinding.getRoot());
            binding = emptyListBinding;
        }
    }

    static class FollowViewHolder extends RecyclerView.ViewHolder {

        ItemFollowRecordBinding binding;

        public FollowViewHolder(ItemFollowRecordBinding itemFollowRecordBinding) {
            super(itemFollowRecordBinding.getRoot());
            binding = itemFollowRecordBinding;
        }
    }
}
